/**
 * 路由配置
 */
export const routePathData = {
  login: '/login', // 用户相关
  register: '/register',
  success: '/success',
  home: '/home',
  userInfo: '/home/userinfo',
  file: '/home/file', // 文件管理
  workflow: '/home/taskflow/list', // 工作流
  buildrecord: '/home/taskflow/record', // 构建记录
  workflowdrawer: '/home/taskflow/drawer', // 工作流画布
  workflowset: '/home/taskflow/set', // 工作流设置
  markVisual: '/home/mark/visual',
  moduleManage: '/home/mark/modulemanage',
  templateManage: '/home/mark/templatemanage',
  linelist: '/home/linemanage/linelist', // 业务线列表
  linedetail: '/home/linemanage/linedetail',
  medallist: '/home/linemanage/medallist', // 勋章管理
  medaldetail: '/home/linemanage/medaldetail',
  previewMark: '/home/mark/preview', // 预览
  appList: '/home/application/appList',
  appVersion: '/home/application/appVersion',
  appBuildLog: '/home/application/appBuildLog',
  deployList: '/home/deploy/deployList',
  createDeploy: '/home/deploy/createDeploy'
}

// 不需要执行全局loading的
export const noLoadingUrl = [
  '/kg/schema/name/hint',
  '/kg/task-types',
  '/kg/pipeline-tasks',
  '/kg/entity/search',
  '/kg/relationship/storage/sreach',
  '/kg/data/property/fuzzy',
  '/knowledge/fusion/tasks',
  '/kg/snapshot/list',
  '/kg/schema/name/translate',
  '/kg/qa/soundToTxt'
]

/**
 * 获取url中的查询参数
 * @param url 要查询的url
 **/
export function getSearchParam (url) {
  if (url.indexOf('?') > -1) {
    let result = {}
    let paraStr = url.split('?')[1]
    let paraItems = paraStr.split('&')

    for (let i = 0; i < paraItems.length; i++) {
      let paraKey = paraItems[i].split('=')[0]
      let paraValue = paraItems[i].split('=')[1]
      result[paraKey] = paraValue
    }
    return result
  } else {
    return null
  }
}

// 深度克隆
export function deepClone (currobj) {
  if (typeof currobj !== 'object') {
    return currobj
  }
  let newObj
  if (currobj instanceof Array) {
    newObj = []
  } else {
    newObj = {}
  }
  for (let key in currobj) {
    if (typeof currobj[key] !== 'object') {
      // 不是引用类型，则复制值
      newObj[key] = currobj[key]
    } else {
      // 引用类型，则递归遍历复制对象
      newObj[key] = deepClone(currobj[key])
    }
  }
  return newObj
}

export function deepCopy (o) {
  if (o instanceof Array) {
    let n = []
    for (let i = 0; i < o.length; ++i) {
      n[i] = deepCopy(o[i])
    }
    return n
  } else if (o instanceof Object) {
    let n = {}
    for (let i in o) {
      n[i] = deepCopy(o[i])
    }
    return n
  } else {
    return o
  }
}

export const addNumberPrefix = num => {
  return num < 10 ? `0${num}` : num
}

// 时间戳替换
export const formatDateByStringMatch = (
  datetime,
  formatString = 'yyyy-MM-dd HH:mm:ss'
) => {
  let date
  if (datetime) {
    date = new Date(datetime)
  }
  return date
    ? formatString.replace(/(yyyy)|(MM)|(dd)|(HH)|(mm)|(ss)/g, match => {
      switch (match) {
        case 'yyyy':
          return date.getFullYear()
        case 'MM':
          return addNumberPrefix(date.getMonth() + 1)
        case 'dd':
          return addNumberPrefix(date.getDate())
        case 'HH':
          return addNumberPrefix(date.getHours())
        case 'mm':
          return addNumberPrefix(date.getMinutes())
        case 'ss':
          return addNumberPrefix(date.getSeconds())
        default:
          return ''
      }
    })
    : undefined
}

// 数组对象属性 查重
export const isArrayPropertyRepeat = (arr, property) => {
  let list = []
  for (let i = 0; i < arr.length; i++) {
    const elememnt = arr[i]
    list.push(elememnt[property])
  }

  return new Set(list).size !== list.length
}

// 多维数组转一维数组
export const getDimensional = arr => {
  let brr = [].concat(arr)
  for (let item of brr) {
    if (item.children) {
      brr = brr.concat(item.children)
      delete item.children
    }
  }
  return brr
}

// 时间戳转字符串
// n=1 显示2011-05-23 15:54:26, n=2: 显示2011-05-23
export function timeToStr (timestamp, n = 1) {
  if (!timestamp) {
    return ''
  }
  timestamp = timestamp + ''
  if (timestamp === undefined) return '-'
  if (timestamp.length <= 10) {
    timestamp = parseInt(timestamp) * 1000
  } else {
    timestamp = parseInt(timestamp) * 1
  }
  var update = new Date(timestamp)
  var year = update.getFullYear()
  var month =
    update.getMonth() + 1 < 10
      ? '0' + (update.getMonth() + 1)
      : update.getMonth() + 1
  var day = update.getDate() < 10 ? '0' + update.getDate() : update.getDate()
  var hour =
    update.getHours() < 10 ? '0' + update.getHours() : update.getHours()
  var minute =
    update.getMinutes() < 10 ? '0' + update.getMinutes() : update.getMinutes()
  var second =
    update.getSeconds() < 10 ? '0' + update.getSeconds() : update.getSeconds()
  if (n === 1) {
    return (
      year +
      '-' +
      month +
      '-' +
      day +
      ' ' +
      hour +
      ':' +
      minute +
      ':' +
      second
    ).toString()
  } else if (n === 2) {
    return (year + '-' + month + '-' + day).toString()
  } else {
    return 0
  }
}

export function formatFileSize (fileSize, idx) {
  var units = ['B', 'KB', 'MB', 'GB']
  idx = idx || 0
  if (fileSize < 1024 || idx === units.length - 1) {
    return fileSize.toFixed(1) + units[idx]
  }
  return formatFileSize(fileSize / 1024, ++idx)
}

// 查询防抖
export const searchDebounce = (func, wait = 50) => {
  let timer = 0

  return function (...args) {
    if (timer) clearTimeout(timer)

    timer = setTimeout(() => {
      func.apply(this, args)
    }, wait)
  }
}

export const intervalThrottle = (fn, t) => {
  let last
  let timer
  let interval = t || 500

  return function () {
    let args = arguments
    let now = +new Date()

    if (last && now - last < interval) {
      clearTimeout(timer)

      timer = setTimeout(() => {
        last = now
        fn.apply(this, args)
      }, interval)
    } else {
      last = now
      fn.apply(this, args)
    }
  }
}

// 判断数据类型
export const typeOf = param => {
  return Object.prototype.toString
    .call(param)
    .toLowerCase()
    .match(/\s+(\w+)/)[1]
}

// 解决名字太长显示问题
export const changeName = (name, len) => {
  if (!name) {
    return ''
  }
  if (len < 1) {
    len = 4
  }
  if (name.length <= len) {
    return name
  }
  name = name.substring(0, len - 1) + '...'
  return name
}

// 数字千分制显示
export const toThousands = num => {
  num = (num || 0).toString()
  let result = ''
  while (num.length > 3) {
    result = ',' + num.slice(-3) + result
    num = num.slice(0, num.length - 3)
  }
  if (num) {
    result = num + result
  }
  return result
}
